#include "Block_Queue.hpp"
#include "Task.hpp"
#include <time.h>
using namespace std;

int add(int x,int y)
{
    return x + y;
}

void* consumer(void* args)
{
    Task ret;
    Block_Queue<Task>* bqueue = (Block_Queue<Task>*)args;
    while(true)
    {
        bqueue->pop(ret);
        cout << pthread_self() <<" consumer: "<< ret._x << "+" << ret._y << "=" << ret() << endl;
        //sleep(1);
    }
    return nullptr;
}

void* producor(void* args)
{
    
    //int a = 0;
    Block_Queue<Task>* bqueue = (Block_Queue<Task>*)args;
    while(true)
    {
        int x = (rand() ^ getpid() ^ 0x3344) % 500;
        usleep(1000);
        int y = (rand() ^ getpid() ^ 0x3344) % 500;
        Task task(x,y,add);
        bqueue->push(task);
        cout <<pthread_self() <<" productor: "<< task._x << "+" << task._y << "=?" << endl;
        //a++;
        sleep(1);
    }
    return nullptr;
}

int main()
{
    srand((unsigned long)time(nullptr));
    Block_Queue<Task>* bqueue = new Block_Queue<Task>;
    pthread_t c,p;
    pthread_create(&c,nullptr,consumer,bqueue);
    pthread_create(&p,nullptr,producor,bqueue);


    pthread_join(c,nullptr);
    pthread_join(p,nullptr);

    delete bqueue;
    return 0;
}